<?php
/**
 * Created by JetBrains PhpStorm.
 * User: 蒋文书
 * Date: 14-12-27
 * Time: 下午11:27
 * To change this template use File | Settings | File Templates.
 */

namespace Admin\Controller;
use Think\Controller;
use Think\Upload;


class ProController extends CommonController{

    /**
     * 分类列表
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function indexClass(){ 
        $this->navList=M('Along')->where(array('type'=>'Pro','lang'=>LANG_SET))->select();
        $map = array();
        $map  = array('p'=>1,'lang'=>LANG_SET,'type'=>1);
        if(isset($_GET['type'])){
            $map['type']   =   I('type'); 
        }
        $this->assign('news_id',I('get.type',1));
        $this -> list = D('Pro')->where($map)->select();
        $this->meta_title = '分类管理';
        $this->display();
    }

    /**
     * 新增分类
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function addClass(){
        if(IS_POST){
            //添加所属角色
            if(D('Pro')->addInfo()){
                 $this->success('新增成功！',U('IndexClass'));
            }else{
                $this->error('新增失败！');
            }
        }
        $this->assign('news_id',I('get.type',1));
        $this->meta_title = '新增分类';
        $this->display();
    }

    /**
     * 删除分类
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function delClass(){  
        $id =I('id',0,'intval');
        if(D('Pro')->del($id)){
            $this->success('删除成功',U('IndexClass'));
        }else{
            $this->error('删除失败！');
        }
    }

    /**
     * 编辑分类
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function editClass(){
        $id = I('id',0,'intval');
        if(IS_POST){
           if(D('Pro')->update()){
                $this->success('更新成功',U('IndexClass'));
            }else{
                $this->error('更新失败！');
            }
        }else{
            $this -> info = D('Pro')->info($id);
        }
        $this -> meta_title = '编辑新闻';
        $this->display();
    }

    /**
     * 新增案例
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function add(){
        $this -> list = D('Pro')->where(array('p'=>1))->select();
        if(IS_POST){
            $setting = C('EDITOR_UPLOAD');
            $this -> uploader = new Upload($setting, 'LOCAL');
            $img   = $this->uploader->upload();
            $data = M('Pro')->create();  
            if($img){
                $data['pic']=$img['pic']['savepath'].$img['pic']['savename'];
                if(D('Pro')->addInfo($data)){
                     $this->success('新增成功！',U('Index'));
                }else{
                     $this->error('新增失败！');
                }
            }else{
                $this->error($this->uploader->getError());
            }
        }
        $this->meta_title = '新增案例';
        $this->display();
    }

    /**
     * 案例列表
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function index(){
        //$this->navList=M('Along')->where(array('type'=>'Pro','lang'=>LANG_SET))->select(); 
        $map = array();
        $map  = array('type'=>1,'p'=>1,'lang'=>LANG_SET);
        if(isset($_GET['type'])){
            $map['type']   =   I('type',1); 
        }
        $this -> listClass = D('Pro')->where($map)->field('id,title,type')->select();
       /* 查询条件初始化 */
        
        $map  = array('p'=>2,'lang'=>LANG_SET);
        if(isset($_GET['pid'])){
            $map['pid']   =   I('pid',0); 
        }
        if(isset($_GET['type'])){
            $map['type']   =   I('type',1); 
        }
        $this -> list = D('Pro')->where($map)->select();

        $this->assign('news_id',I('get.type',1));

        $this->meta_title = '产品管理';
        $this->display();
    }

    /**
     * 产品编辑
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */

    public function edit(){
        $id = I('id',0,'intval');
        if(IS_POST){
            $setting = C('EDITOR_UPLOAD');
            $this -> uploader = new Upload($setting, 'LOCAL');
            $img   = $this->uploader->upload();
            $data = M('Pro')->create(); 
            if($img){ 
                $data['pic']=$img['pic']['savepath'].$img['pic']['savename'];
                if(M('Pro')->save($data)){
                     $this->success('更新成功',U('Index'));
                }else{
                     $this->error('更新失败！');
                }
            }else{
               if(M('Pro')->save($data)){
                    $this->success('更新成功',U('Index'));
                }else{
                    $this->error('更新失败！');
                }
            }
        }else{
            $this -> list = D('Pro')->where(array('p'=>1))->select();
            $this -> info = D('Pro')->info($id);
        }
        $this -> meta_title = '编辑案例';
        $this->display();
    }

    /**
     * 删除案例
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function del(){  
        $id =I('id',0,'intval');
        if(D('Pro')->del($id)){
            $this->success('删除成功',U('Index'));
        }else{
            $this->error('删除失败！');
        }
    }

    /**
     * 分类联动
     * @author 蒋文书 <jiangwenshu1990@126.com>
     */   
    public function cate(){
        $id=I('id',0,'intval');
        $data =M('Pro')->where(array('type'=>$id,'p'=>1,'status'=>1))->field('id,title,type')->select();
        dump($data); die();
        $this->ajaxReturn($data);
    }

    /**
     * ueditor
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function editor(){
        $setting = C('EDITOR_UPLOAD');
        $this -> uploader = new Upload($setting, 'LOCAL');
        $img   = $this->uploader->upload();
        $return = array();
        $return['url'] = $img['upfile']['savepath'].$img['upfile']['savename'];
        $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
        $return['title'] = $title;
        $return['original'] = $img['upfile']['name'];
        $return['state'] = ($img)? 'SUCCESS' : $this->uploader->getError();
        $this->ajaxReturn($return);
    }
    
}